package com.szh.zx.javacorecorret.chap2.one;
// 章节 2.1.5
// 方法  public boolean await(long timeout, TimeUnit unit)的作用是线程在指定的最大时间单位内进入waiting状态，如果超过这个时间则自动唤醒，程序继续向下运行。

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchTest5 {
	public static void main(String[] args) {
		MyService5 service = new MyService5();
		MyThread5[] threadArray = new MyThread5[3];
		for (int i = 0; i < threadArray.length; i++) {
			threadArray[i]  = new MyThread5(service);
		}
		for (int i = 0; i < threadArray.length; i++) {
			threadArray[i].start();
		}
	}
}
class MyService5{
	public CountDownLatch count = new CountDownLatch(1);
	public void testMethod(){
		try {
			System.out.println(Thread.currentThread().getName()+"准备"+System.currentTimeMillis());
			// 阻塞到 计数为零
			count.await(3, TimeUnit.SECONDS);
			System.out.println(Thread.currentThread().getName()+"结束"+System.currentTimeMillis());

		}catch (InterruptedException e){
			e.printStackTrace();
		}
	}
}

class MyThread5 extends Thread{
	private MyService5 service;

	public MyThread5(MyService5 service) {
		this.service = service;
	}

	@Override
	public void run() {
		service.testMethod();
	}
}